Equality Checks
Kotlin uses ==
for structural comparison and ===
for referential comparison.
More precisely, a == b
compiles down to if (a == null) b == null else a.equals(b)
.
fun main() {
val authors = setOf("Shakespeare", "Hemingway", "Twain")
val writers = setOf("Twain", "Shakespeare", "Hemingway")
println(authors == writers) // 1
println(authors === writers) // 2
}
- Returns
true
because it callsauthors.equals(writers)
and sets ignore element order. - Returns
false
becauseauthors
andwriters
are distinct references.